package com.xyz.aclservice.service.impl;

import com.xyz.aclservice.entity.EduRole;
import com.xyz.aclservice.entity.EduUserRole;
import com.xyz.dao.EduRoleMapper;
import com.xyz.aclservice.service.EduRoleService;
import com.xyz.aclservice.service.EduUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
public class EduRoleServiceImpl extends ServiceImpl<EduRoleMapper, EduRole> implements EduRoleService {

    @Autowired
    private EduUserRoleService eduUserRoleService;


    //根据用户获取角色数据
    @Override
    public Map<String, Object> findRoleByUserId(String userId) {
        //查询所有的角色
        List<EduRole> allRolesList =baseMapper.selectList(null);

        //根据用户id，查询用户拥有的角色id
        List<EduUserRole> existEduUserRoleList = eduUserRoleService.list(new QueryWrapper<EduUserRole>().eq("user_id", userId).select("role_id"));

        List<String> existRoleList = existEduUserRoleList.stream().map(c->c.getRoleId()).collect(Collectors.toList());

        //对角色进行分类
        List<EduRole> assignEduRoles = new ArrayList<EduRole>();
        for (EduRole eduRole : allRolesList) {
            //已分配
            if(existRoleList.contains(eduRole.getId())) {
                assignEduRoles.add(eduRole);
            }
        }

        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assignRoles", assignEduRoles);
        roleMap.put("allRolesList", allRolesList);
        return roleMap;
    }

    //根据用户分配角色
    @Override
    public void saveUserRoleRealtionShip(String userId, String[] roleIds) {
        eduUserRoleService.remove(new QueryWrapper<EduUserRole>().eq("user_id", userId));

        List<EduUserRole> eduUserRoleList = new ArrayList<>();
        for(String roleId : roleIds) {
            if(StringUtils.isEmpty(roleId)) continue;
            EduUserRole eduUserRole = new EduUserRole();
            eduUserRole.setUserId(userId);
            eduUserRole.setRoleId(roleId);

            eduUserRoleList.add(eduUserRole);
        }
        eduUserRoleService.saveBatch(eduUserRoleList);
    }

    @Override
    public List<EduRole> selectRoleByUserId(String id) {
        //根据用户id拥有的角色id
        List<EduUserRole> eduUserRoleList = eduUserRoleService.list(new QueryWrapper<EduUserRole>().eq("user_id", id).select("role_id"));
        List<String> roleIdList = eduUserRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
        List<EduRole> eduRoleList = new ArrayList<>();
        if(roleIdList.size() > 0) {
            eduRoleList = baseMapper.selectBatchIds(roleIdList);
        }
        return eduRoleList;
    }
}
